//
//  GYMainShopSelectCell.m
//  GoodLuck
//
//  Created by apple on 2018/2/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainShopSelectCell.h"
#import "GYNetWorkTools.h"

static NSString *selectCellIdentifier = @"selectCellIdentifier";

@interface GYMainShopSelectCell ()

@property (weak, nonatomic) IBOutlet UILabel *goodNameLable;
@property (weak, nonatomic) IBOutlet UILabel *goodsDesLable;
@property (weak, nonatomic) IBOutlet UILabel *goodsPriceLable;
@property (weak, nonatomic) IBOutlet UILabel *goodsSellLable;

@property (weak, nonatomic) IBOutlet UILabel *addressLable;
@property (weak, nonatomic) IBOutlet UILabel *freightLable;
@property (weak, nonatomic) IBOutlet UILabel *defaultLable;
@property (weak, nonatomic) IBOutlet UILabel *stockLable;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *addressLableheightCons;
@property (weak, nonatomic) IBOutlet UILabel *originalPriceLable;


@end

@implementation GYMainShopSelectCell

- (IBAction)onClick:(UIButton *)sender {
    self.selectGoodsDistrbutionAddressCallBack();
}

- (IBAction)defaultBtuClick:(UIButton *)sender {
    self.defaultBtuClickCallBack();
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.addressLable.preferredMaxLayoutWidth = ScreenWidth-110;
    
    //监听商品选择数量发生改变的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateSelectedGoodsNum:) name:@"updateSelectedGoodsNum" object:nil];
}

- (void)updateSelectedGoodsNum:(NSNotification *)noti {
    int goodsNum =[noti.object intValue];
    NSDictionary *goods_info = [self.goodsDetails objectForKey:@"goods_info"];
    self.defaultLable.text = [NSString stringWithFormat:@"%@ %d",[goods_info objectForKey:@"goods_name"],goodsNum];
}

+ (instancetype)mainShopSelectCellWithTableView:(UITableView *)tableView {
    GYMainShopSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:selectCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([GYMainShopSelectCell class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (void)setGoodsDetails:(NSDictionary *)goodsDetails {
    _goodsDetails = goodsDetails;
    //NSLog(@"goodsDetails=============%@",goodsDetails);
    //赋值
    NSDictionary *goods_info = [goodsDetails objectForKey:@"goods_info"];
    self.goodNameLable.text = [goods_info objectForKey:@"goods_name"];
    self.goodsDesLable.text = [goods_info objectForKey:@"goods_jingle"];
    self.goodsPriceLable.text = [NSString stringWithFormat:@"¥ %@",[goods_info objectForKey:@"goods_price"] == nil ? @"" : [goods_info objectForKey:@"goods_price"]];
    //设置原价
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSString *textStr = [NSString stringWithFormat:@"¥ %@",[goods_info objectForKey:@"goods_marketprice"]];
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];
    self.originalPriceLable.attributedText = attribtStr;
    
    self.goodsSellLable.text = [NSString stringWithFormat:@"销量:%@件",[goods_info objectForKey:@"goods_salenum"]];
    
    NSDictionary *goods_hair_info = [goodsDetails objectForKey:@"goods_hair_info"];
    self.addressLable.text = [goods_hair_info objectForKey:@"area_name"];
    
    int goods_storage = [[goods_info objectForKey:@"goods_storage"] intValue];
    if (goods_storage > 0) {
        self.stockLable.text = [goods_hair_info objectForKey:@"if_store_cn"];
    }else {
        self.stockLable.text = @"库存不足";
    }
    
    //设置默认按钮的标题
    self.defaultLable.text = [NSString stringWithFormat:@"%@ 1",[goods_info objectForKey:@"goods_name"]];
    
    //设置用户默认地址
    if ([[goodsDetails objectForKey:@"default_address"] isKindOfClass:[NSDictionary class]]) {
        NSDictionary *default_address = [goodsDetails objectForKey:@"default_address"];
        if (default_address == nil) {
            self.addressLable.text = @"全国";
            self.addressLableheightCons.constant = 0;
        }else {
            self.addressLable.text = [default_address objectForKey:@"area_info"];
            
            //如果用户默认的地址存在的话, 就请求运费
            self.addressLableheightCons.constant = 15;
            NSString *area_id = [default_address objectForKey:@"area_id"];
            //这个设置方法要执行3遍  有待优化/////////////////////////
            [self getCurrentAddressFreight:area_id];
        }
    }
}

- (CGFloat)cellHeight:(NSDictionary *)model {
    self.goodsDetails = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.defaultLable.frame) + 15;
}

- (void)setSelectAddress:(NSString *)selectAddress {
    _selectAddress = selectAddress;
    
    //商品的运费
    if (selectAddress != nil && selectAddress.length != 0) {
        self.addressLable.text = selectAddress;
        self.addressLableheightCons.constant = 15;
        
        [self layoutIfNeeded];
    }
}

- (void)setSelectAread_id:(NSString *)selectAread_id {
    _selectAread_id = selectAread_id;
    if (selectAread_id != nil && selectAread_id.length != 0) {
        [self getCurrentAddressFreight:selectAread_id];
    }
}

- (void)getCurrentAddressFreight:(NSString *)area_id {
    NSDictionary *goods_info = [self.goodsDetails objectForKey:@"goods_info"];
    NSString *goods_id = [goods_info objectForKey:@"goods_id"];
    [GYNetWorkTools requestGoodsFreight:goods_id area_id:area_id success:^(NSDictionary *obj) {
        //NSLog(@"=========obj============%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            NSDictionary *datas = [obj objectForKey:@"datas"];
            self.freightLable.text = [datas objectForKey:@"content"];
        }
    } error:^(NSError *error) {
        NSLog(@"requestGoodsFreight=======%@",error);
    }];
}
@end
